import request from '../utls/require'

// 社区相关接口

/**
 * 获取帖子列表
 */
export const getPosts = (params?: any) => {
  console.log('[API] [Community] 开始发送获取帖子列表请求', params);
  return request({
    url: '/xiangsu/posts/',
    method: 'get',
    params
  }).then(response => {
    console.log('[API] [Community] 获取帖子列表响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 获取帖子列表请求失败', error);
    throw error;
  });
}

/**
 * 发布新帖子
 */
export const createPost = (data: any) => {
  console.log('[API] [Community] 开始发送发布新帖子请求', data);
  return request({
    url: '/xiangsu/posts/',
    method: 'post',
    data
  }).then(response => {
    console.log('[API] [Community] 发布新帖子响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 发布新帖子请求失败', error);
    throw error;
  });
}

/**
 * 获取帖子详情
 */
export const getPostDetail = (id: number) => {
  console.log('[API] [Community] 开始发送获取帖子详情请求', id);
  return request({
    url: `/xiangsu/posts/${id}/`,
    method: 'get'
  }).then(response => {
    console.log('[API] [Community] 获取帖子详情响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 获取帖子详情请求失败', error);
    throw error;
  });
}

/**
 * 删除帖子
 */
export const deletePost = (id: number) => {
  console.log('[API] [Community] 开始发送删除帖子请求', id);
  return request({
    url: `/xiangsu/posts/${id}/`,
    method: 'delete'
  }).then(response => {
    console.log('[API] [Community] 删除帖子响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 删除帖子请求失败', error);
    throw error;
  });
}

/**
 * 获取当前用户帖子列表
 */
export const getUserPosts = (params?: any) => {
  console.log('[API] [Community] 开始发送获取当前用户帖子列表请求', params);
  return request({
    url: '/xiangsu/user/posts/',
    method: 'get',
    params
  }).then(response => {
    console.log('[API] [Community] 获取当前用户帖子列表响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 获取当前用户帖子列表请求失败', error);
    throw error;
  });
}

/**
 * 点赞帖子
 */
export const likePost = (id: number) => {
  console.log('[API] [Community] 开始发送点赞帖子请求', id);
  return request({
    url: `/xiangsu/posts/${id}/like/`,
    method: 'post'
  }).then(response => {
    console.log('[API] [Community] 点赞帖子响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 点赞帖子请求失败', error);
    throw error;
  });
}

/**
 * 取消点赞帖子
 */
export const unlikePost = (id: number) => {
  console.log('[API] [Community] 开始发送取消点赞帖子请求', id);
  return request({
    url: `/xiangsu/posts/${id}/like/`,
    method: 'delete'
  }).then(response => {
    console.log('[API] [Community] 取消点赞帖子响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 取消点赞帖子请求失败', error);
    throw error;
  });
}

/**
 * 获取帖子点赞列表
 */
export const getPostLikes = (id: number, params?: any) => {
  console.log('[API] [Community] 开始发送获取帖子点赞列表请求', id, params);
  return request({
    url: `/xiangsu/posts/${id}/likes/`,
    method: 'get',
    params
  }).then(response => {
    console.log('[API] [Community] 获取帖子点赞列表响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 获取帖子点赞列表请求失败', error);
    throw error;
  });
}

/**
 * 获取评论列表
 */
export const getComments = (postId: number, params?: any) => {
  console.log('[API] [Community] 开始发送获取评论列表请求', postId, params);
  return request({
    url: `/xiangsu/posts/${postId}/comments/`,
    method: 'get',
    params
  }).then(response => {
    console.log('[API] [Community] 获取评论列表响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 获取评论列表请求失败', error);
    throw error;
  });
}

/**
 * 发表评论
 */
export const createComment = (postId: number, data: any) => {
  console.log('[API] [Community] 开始发送发表评论请求', postId, data);
  return request({
    url: `/xiangsu/posts/${postId}/comments/`,
    method: 'post',
    data
  }).then(response => {
    console.log('[API] [Community] 发表评论响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 发表评论请求失败', error);
    throw error;
  });
}

/**
 * 删除评论
 */
export const deleteComment = (commentId: number) => {
  console.log('[API] [Community] 开始发送删除评论请求', commentId);
  return request({
    url: `/xiangsu/comments/${commentId}/`,
    method: 'delete'
  }).then(response => {
    console.log('[API] [Community] 删除评论响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 删除评论请求失败', error);
    throw error;
  });
}

/**
 * 上传动态图片（支持进度监听）
 */
export const uploadPostImages = (formData: FormData, onProgress?: (progress: number) => void) => {
  console.log('[API] [Community] 开始发送上传动态图片请求');
  return request({
    url: '/xiangsu/posts/upload-images/',
    method: 'post',
    data: formData,
    // 进度回调
    onUploadProgress: (evt: any) => {
      if (onProgress && evt && evt.total) {
        const progress = Math.round((evt.loaded / evt.total) * 100);
        onProgress(progress);
      }
    }
  }).then(response => {
    console.log('[API] [Community] 上传动态图片响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 上传动态图片请求失败', error);
    throw error;
  });
}

/**
 * 获取标签列表
 */
export const getTags = () => {
  console.log('[API] [Community] 开始发送获取标签列表请求');
  return request({
    url: '/xiangsu/tags/',
    method: 'get'
  }).then(response => {
    console.log('[API] [Community] 获取标签列表响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] 获取标签列表请求失败', error);
    throw error;
  });
}

/**
 * AI润色文本
 */
export const polishPostText = (data: { text: string }) => {
  console.log('[API] [Community] 开始发送AI润色请求', data);
  return request({
    url: '/xiangsu/posts/polish/',
    method: 'post',
    data
  }).then(response => {
    console.log('[API] [Community] AI润色响应成功', response);
    return response;
  }).catch(error => {
    console.error('[API] [Community] AI润色请求失败', error);
    throw error;
  });
}

// 环保动态资讯
export const getEcoNews = (params?: any) => {
  console.log('[API] [Community] 开始发送获取环保动态请求', params);
  return request({
    url: '/xiangsu/news/',
    method: 'get',
    params
  }).then(resp => {
    console.log('[API] [Community] 获取环保动态响应成功', resp);
    return resp;
  }).catch(err => {
    console.error('[API] [Community] 获取环保动态请求失败', err);
    throw err;
  });
}
